<?php
/* 	These code was generated using phpCIGen v 0.1.b (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	
	+ Module  		: users Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_users.php
 	+ Author  		: zainal, mukhlison
 	+ Created on 16/Jul/2009 15:35:27
	
*/

//class of users
class C_tbl_s_users extends CI_Controller {

	//constructor
	function C_tbl_s_users(){
		parent::__construct();
		$this->load->model('m_tbl_s_users', '', TRUE);

		
		// if(!$_SESSION[SESSION_USERID])
			// redirect('c=c_login', 'location', 301);
			checkLogin();
	}
	
	function get_usergroups_list(){
		$result = $this->m_public_function->get_usergroups_list();
		echo $result;
	}
	
	function get_group_list(){
		$result = $this->m_public_function->get_group_list();
		echo $result;
	}

	//set index
	function index(){
		$this->load->view('main/v_tbl_s_users');
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->users_list();
				break;
			case "UPDATE":
				$this->users_update();
				break;
			case "CREATE":
				$this->users_create();
				break;
			case "DELETE":
				$this->users_delete();
				break;
			case "SEARCH":
				$this->users_search();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	//function fot list record
	function users_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);

		$result=$this->m_tbl_s_users->users_list($query,$start,$end);
		echo $result;
	}

	//function for update record
	function users_update(){
		//POST variable here
		$user_id=trim(@$_POST["user_id"]);
		$user_name=trim(@$_POST["user_name"]);
		$user_name=str_replace("/(<\/?)(p)([^>]*>)", "",$user_name);
		$user_name=str_replace("'", '"',$user_name);
		$user_passwd=trim(@$_POST["user_passwd"]);
		$user_passwd=str_replace("/(<\/?)(p)([^>]*>)", "",$user_passwd);
		$user_passwd=str_replace("'", '"',$user_passwd);
		$user_karyawan=trim(@$_POST["user_karyawan"]);
		//$user_log=trim(@$_POST["user_log"]);
		$user_groups=trim(@$_POST["user_groups"]);
		$user_aktif=trim(@$_POST["user_aktif"]);
		$user_aktif=str_replace("/(<\/?)(p)([^>]*>)", "",$user_aktif);
		$user_aktif=str_replace("'", '"',$user_aktif);
		$user_login=trim(@$_POST["user_login"]);
		$user_logout=trim(@$_POST["user_logout"]);
		$user_cabang=trim(@$_POST["user_cabang"]);
		
		$result = $this->m_tbl_s_users->users_update($user_id, $user_name, $user_passwd, $user_karyawan, 
														$user_groups, $user_cabang, 
													 $user_login, $user_logout, $user_aktif );
		echo $result;
	}
	
	//function for create new record
	function users_create(){
		//POST varible here
		//auto increment, don't accept anything from form values
		$user_name=trim(@$_POST["user_name"]);
		$user_name=str_replace("/(<\/?)(p)([^>]*>)", "",$user_name);
		$user_name=str_replace("'", '"',$user_name);
		$user_passwd=trim(@$_POST["user_passwd"]);
		$user_passwd=str_replace("/(<\/?)(p)([^>]*>)", "",$user_passwd);
		$user_passwd=str_replace("'", '"',$user_passwd);
		$user_karyawan=trim(@$_POST["user_karyawan"]);
		$user_log=trim(@$_POST["user_log"]);
		$user_groups=trim(@$_POST["user_groups"]);
		$user_aktif=trim(@$_POST["user_aktif"]);
		$user_aktif=str_replace("/(<\/?)(p)([^>]*>)", "",$user_aktif);
		$user_aktif=str_replace("'", '"',$user_aktif);
		$user_login=trim(@$_POST["user_login"]);
		$user_logout=trim(@$_POST["user_logout"]);
		$user_cabang=trim(@$_POST["user_cabang"]);
		
		$result=$this->m_tbl_s_users->users_create($user_name ,$user_passwd ,$user_karyawan ,$user_log ,
													$user_groups,$user_cabang, $user_login, $user_logout
												   ,$user_aktif );
		echo $result;
	}

	//function for delete selected record
	function users_delete(){
		$ids = $_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_tbl_s_users->users_delete($pkid);
		echo $result;
	}

	//function for advanced search
	function users_search(){
		//POST varibale here
		$user_name=trim(@$_POST["user_name"]);
		$user_name=str_replace("/(<\/?)(p)([^>]*>)", "",$user_name);
		$user_name=str_replace("'", '"',$user_name);
		$user_groups=trim(@$_POST["user_groups"]);
		$user_aktif=trim(@$_POST["user_aktif"]);
		$user_aktif=str_replace("/(<\/?)(p)([^>]*>)", "",$user_aktif);
		$user_aktif=str_replace("'", '"',$user_aktif);
		
		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$result = $this->m_tbl_s_users->users_search($user_name ,$user_groups ,$user_aktif ,$start,$end);
		echo $result;
	}

	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Encodes a YYYY-MM-DD into a MM-DD-YYYY string
	function codeDate ($date) {
	  $tab = explode ("-", $date);
	  $r = $tab[1]."/".$tab[2]."/".$tab[0];
	  return $r;
	}
	
}
?>